﻿using System;
using System.Windows;
using System.Windows.Media;
using GalaSoft.MvvmLight.CommandWpf;
using GalaSoft.MvvmLight.Messaging;
using SpreadsheetGear;
using SpreadsheetGear.Commands;
using SpreadsheetGear.Windows.Controls;
using WPFWorkbookDesigner.Addons;
using WPFWorkbookDesigner.Events;
using WPFWorkbookDesigner.Model.SpreadSheet;
using WPFWorkbookDesigner.UI;
using WPFWorkbookDesigner.ViewModel;

namespace WPFWorkbookDesigner.Model.Commands
{
    public static class EditMenu
    {
        #region Commands

        public static RelayCommand<WorkbookView> ClearAllCommand { get; set; } =
            new RelayCommand<WorkbookView>(ClearAll, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> ClearCommentsCommand { get; set; }

        public static RelayCommand<WorkbookView> ClearContentCommand { get; set; } =
            new RelayCommand<WorkbookView>(ClearContents, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> ClearFormatsCommand { get; set; } =
            new RelayCommand<WorkbookView>(ClearFormats, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> CopyCommand { get; } =
            new RelayCommand<WorkbookView>(Edit_Copy, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> CopySheetCommand { get; set; } =
            new RelayCommand<WorkbookView>(CopySheet);

        public static RelayCommand<WorkbookView> CutCommand { get; } =
            new RelayCommand<WorkbookView>(Edit_Cut, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> DeleteCellsCommand { get; set; } =
            new RelayCommand<WorkbookView>(DeleteCells);

        public static RelayCommand<WorkbookView> DeleteWorksheetCommand { get; set; } =
            new RelayCommand<WorkbookView>(DeleteWorksheet);

        public static RelayCommand<WorkbookView> FillDownCommand { get; set; } =
            new RelayCommand<WorkbookView>(RangeFillDown, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> FillRightCommand { get; set; } =
            new RelayCommand<WorkbookView>(RangeFillRight, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> PasteCommand { get; } =
            new RelayCommand<WorkbookView>(Edit_Paste, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> PasteSepcialCommand { get; set; } =
            new RelayCommand<WorkbookView>(PasteSpecial);

        public static RelayCommand<WorkbookView> RedoCommand { get; } =
            new RelayCommand<WorkbookView>(Redo, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> UndoCommand { get; } =
            new RelayCommand<WorkbookView>(Undo, MenuChecker.IsNotEditing);

        public static RelayCommand<WorkbookView> FindCommand { get; } =
            new RelayCommand<WorkbookView>(Find, MenuChecker.IsNotEditing);
        public static RelayCommand<WorkbookView> ReplaceCommand { get; } =
            new RelayCommand<WorkbookView>(Replace, MenuChecker.IsNotEditing);

        #endregion

        private static void ClearAll(WorkbookView view)
        {
            var command = view.ActiveCommandManager.CreateCommandClear(view.RangeSelection);
            view.ActiveCommandManager.Execute(command);
        }

        private static void ClearComments(WorkbookView view)
        {

        }

        private static void ClearContents(WorkbookView view)
        {
            var command = view.ActiveCommandManager.CreateCommandClearContents(view.RangeSelection);
            view.ActiveCommandManager.Execute(command);
        }

        private static void ClearFormats(WorkbookView view)
        {
            var c = view.ActiveCommandManager.CreateCommandClearFormats(view.RangeSelection);
            view.ActiveCommandManager.Execute(c);
        }

        private static void CopySheet(WorkbookView view)
        {
            var active = view.ActiveSheet;
            active.CopyAfter(active);
        }

        private static void DeleteCells(WorkbookView view)
        {
        }

        private static void DeleteWorksheet(WorkbookView view)
        {
            var active = view.ActiveWorksheet;
            try
            {
                active.Delete();
            }
            catch (InvalidOperationException)
            {
                MesBox.Error("无法删除工作簿中最后一张可见的工作表");
            }
        }

        private static void Edit_Copy(WorkbookView view)
        {
            IRange range = view.RangeSelection;

            view.ActiveCommandManager.Execute(new CommandRange.Copy(range));
        }

        private static void Edit_Cut(WorkbookView view)
        {
            view.ActiveCommandManager.Execute(new CommandRange.Cut(view.RangeSelection));
        }

        private static void Edit_Paste(WorkbookView view)
        {
            view.ActiveCommandManager.Execute(new CommandRange.Paste(view.RangeSelection));
        }

        private static void PasteSpecial(WorkbookView view)
        {
            PasteSpecialWindow.GetInstance();
            Messenger.Default.Send<BroadcastActiveViewEvent, PasteSpecialViewModel>(new BroadcastActiveViewEvent(view));
        }

        private static void Find(WorkbookView view)
        {
            FindReplaceWindow.GetInstance();
            Messenger.Default.Send<BroadcastActiveViewEvent, FindReplaceViewModel>(new BroadcastActiveViewEvent(view));
        }
        private static void Replace(WorkbookView view)
        {
            FindReplaceWindow.GetInstance();
            Messenger.Default.Send<BroadcastActiveViewEvent, FindReplaceViewModel>(new BroadcastActiveViewEvent(view));
        }

        private static void RangeFillDown(WorkbookView view)
        {
            var range = view.RangeSelection;
            view.ActiveCommandManager.Execute(new UndoableFill(range, FillDirection.Down));
        }

        private static void RangeFillRight(WorkbookView view)
        {
            var range = view.RangeSelection;
            view.ActiveCommandManager.Execute(new UndoableFill(range, FillDirection.Right));
        }

        private static void Redo(WorkbookView view)
        {
            if (view.ActiveCommandManager.CanRedo)
                view.ActiveCommandManager.Redo();
            else
                MesBox.Info("没有可以重做的操作!");
        }

        private static void Undo(WorkbookView view)
        {
            if (view.ActiveCommandManager.CanUndo)
                view.ActiveCommandManager.Undo();
            else
                MesBox.Info("没有可以撤销的操作!");
        }
    }
}